#---------------------------------#
# Between group social distance   #
#---------------------------------#
rm(list = ls())

polity.diffusion <- readRDS("data/polity_diffusion_replication.rds")
adjacency_matricies <- readRDS("intl_order/adjacency_matricies_replication.rds")

assortativity_regime <- c()
assortativity_dim1   <- c()
assortativity_dim2   <- c()
assortativity_group  <- c()
assortativity_ideol  <- c()
assortativity_degree <- c()
transitivity_measure <- c()
modularity_measure   <- c()
n_states             <- c()
prop_democ           <- c()
net_density          <- c()
for (i in 1:length(adjacency_matricies))
{
  g <- graph_from_adjacency_matrix(adjacency_matricies[[i]], mode = "undirected", weighted = T)
  polity_temp <- filter(
    polity.diffusion,
    year == i + min(polity.diffusion$year) - 1
  ) %>%  dplyr::rename(
      name = names
    ) %>% dplyr::select(
      c(
        name, polity, membership
      )
    ) %>% mutate(
      polity = as.numeric(as.character(polity)),
      gov = "Mixed regime", 
      gov = ifelse(polity >= 6, "Democratic", gov),
      gov = ifelse(polity <= -6, "Autocratic", gov)
    ) %>% arrange(
      name
    ) %>% arrange(
      name
    )
  
  g <- as_tbl_graph(g)
  g <- g %>% activate(
      nodes
    ) %>% left_join(
      polity_temp,
      by = "name"
    ) 
  g <- as.igraph(g)
  
  assortativity_regime[i]   <- assortnet::assortment.discrete(adjacency_matricies[[i]], polity_temp$gov, weighted = TRUE)$r
  assortativity_group[i]    <- assortnet::assortment.discrete(adjacency_matricies[[i]], as.character(polity_temp$membership), weighted = TRUE)$r
  assortativity_degree[i]   <- igraph::assortativity_degree(g, directed = F)
  transitivity_measure[i]   <- igraph::transitivity(g, weights = as.numeric(E(g)$weight)) 
  modularity_measure[i]     <- igraph::modularity(g, membership = as.numeric(V(g)$membership), weights = as.numeric(E(g)$weight))
  n_states[i]               <- nrow(polity_temp)
  net_density[i]            <- edge_density(g)
  prop_democ[i] <- sum(polity_temp$polity >= 6)/nrow(polity_temp)
}

yearly_between_group_distance = data.frame(
  assortativity_regime = assortativity_regime,
  assortativity_regime_normalized = scale(assortativity_regime),
  assortativity_group = assortativity_group,
  assortativity_group_normalized = scale(assortativity_group),
  assortativity_degree = assortativity_degree,
  assortativity_degree_normalized = scale(assortativity_degree),
  n_states = n_states,
  density = net_density, 
  transitivity = transitivity_measure,
  modularity = modularity_measure,
  prop_democ = prop_democ,
  year = 1816:2014
)

yearly_between_group_distance[is.na(yearly_between_group_distance)] <- 0
cor(yearly_between_group_distance$assortativity_regime, yearly_between_group_distance$modularity)

saveRDS(yearly_between_group_distance, file = "data/yearly_between_group_distance_replication.rds")


desc_states <- 
  data.frame(
    min_year = c(1816, seq(1831, 2001, 10)),
    max_year = c(seq(1830, 2014, 10)),
    transitivity = rep(NA, 19),
    degree_assortativity = rep(NA, 19),
    modularity = rep(NA, 19),
    density = rep(NA, 19),
    states = rep(NA, 19),
    groups = rep(NA, 19),
    new_groups = rep(NA, 19)
  )


n_membership <- mutate(
  polity.diffusion,
  n = 1
) %>% aggregate(
  n ~ membership + year, data = ., FUN = sum
)


polity.diffusion <- left_join(
  polity.diffusion, 
  n_membership, 
  by = c(
    "membership", "year"
  )
)


for (i in 1:nrow(desc_states))
{
  desc_states$modularity[i] <- round(median(subset(yearly_between_group_distance, year %in%  desc_states$min_year[i]:desc_states$max_year[i])$modularity, na.rm = T), 2)
  desc_states$transitivity[i] <- round(median(subset(yearly_between_group_distance, year %in%  desc_states$min_year[i]:desc_states$max_year[i])$transitivity, na.rm = T), 2)
  desc_states$degree_assortativity[i] <- round(median(subset(yearly_between_group_distance, year %in%  desc_states$min_year[i]:desc_states$max_year[i])$assortativity_degree, na.rm = T), 2)
  desc_states$density[i] <- round(median(subset(yearly_between_group_distance, year %in%  desc_states$min_year[i]:desc_states$max_year[i])$density, na.rm = T), 2)
  desc_states$states[i] <- length(unique(subset(polity.diffusion, year %in% desc_states$min_year[i]:desc_states$max_year[i])$names))
  desc_states$groups[i] <- length(unique(subset(polity.diffusion, year %in% desc_states$min_year[i]:desc_states$max_year[i])$membership))
  
  if (i != 1)
  {
    desc_states$new_groups[i] <- sum(!(unique(subset(polity.diffusion, year %in% desc_states$min_year[i]:desc_states$max_year[i])$membership) %in% 
                                         unique(subset(polity.diffusion, year %in% desc_states$min_year[i - 1]:desc_states$max_year[i - 1])$membership)))
  }
}

cor(desc_states$transitivity, desc_states$density)
cor(desc_states$groups, desc_states$states)

for (i in 3:ncol(desc_states))
{
  desc_states[,i] <- paste0("&$", desc_states[,i], "$")
}

desc_states[,i] <- paste0(  desc_states[,i], "\\\\")


desc_states <- dplyr::select(
  desc_states, 
  c(
    min_year, max_year, states, transitivity, density, modularity, groups, new_groups
  )
)

export_desc <- print(desc_states, row.names = F)
write.table(export_desc, file = "data_visualization/table_si_6.txt", row.names = F)
